﻿using System;
using System.Collections;

namespace Program22
{
    class Program
    {
     
            /// <summary>
            ///  两个数组的并集
            /// </summary>
            /// <param name="args"></param>
            private static void Main(string[] args)
            {
                int[] nums1 = new int[10];
                int[] nums2 = new int[6];
                ArrayList arr = new ArrayList();
                Random rnd = new Random();
                Console.WriteLine("初始化数据A");
                while (arr.Count < 20)
                {
                    int tempNum = rnd.Next(1, 30);
                    arr.Add(tempNum);
                }
                for (var i = 0; i < nums1.Length; i++)
                {
                    nums1[i] = (int)arr[i];
                    Console.Write(nums1[i] + " ");
                }
                Console.WriteLine();
                Console.WriteLine("初始化数据B");
                arr = new ArrayList();
                while (arr.Count < 8)
                {
                    int tempNum = rnd.Next(1, 20);
                    arr.Add(tempNum);
                }
                for (var i = 0; i < nums2.Length; i++)
                {
                    nums2[i] = (int)arr[i];
                    Console.Write(nums2[i] + " ");
                }
                Console.WriteLine();

                //合并，求并集
                int[] d = new int[20];   //并集
                int index = 0;  //插入d的位置
                var flag = true; //是否有重复元素的标识，如果flag=true则表示无重复可以添加到d
                                 //先遍历nums1，把不重复的元素添加到d
                for (var n = 0; n < nums1.Length; n++)
                {
                    //检查d在index之前是否有重复元素
                    for (var m = 0; m < index; m++)
                    {
                        if (d[m] == nums1[n])
                            flag = false;
                    }

                    //如果没有重复，添加此nums1元素到d的index位置
                    if (flag)
                    {
                        d[index++] = nums1[n];
                    }
                    //flag重置
                    flag = true;
                }
                //flag重置
                flag = true;
                //先遍历nums2，把不重复的元素添加到d
                for (var n = 0; n < nums2.Length; n++)
                {
                    //检查d在index之前是否有重复元素
                    for (var m = 0; m < index; m++)
                    {
                        if (d[m] == nums2[n])
                            flag = false;
                    }

                    //如果没有重复，添加此nums2元素到d的index位置
                    if (flag)
                    {
                        d[index++] = nums2[n];
                    }

                    //flag重置
                    flag = true;
                }

                Console.WriteLine();
                Console.WriteLine("Nums1和Nums2的并集：");
                for (var i = 0; i < 20; i++)
                {
                    if (d[i] != 0)
                        Console.Write(d[i] + " ");
                }

                //交集
            }
        }
    }